package com.ruoyi.supplier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.supplier.domain.bo.ProductBo;
import com.ruoyi.supplier.domain.vo.ProductVo;
import com.ruoyi.supplier.domain.Product;
import com.ruoyi.supplier.mapper.ProductMapper;
import com.ruoyi.supplier.service.IProductService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class ProductServiceImpl implements IProductService {

    private final ProductMapper baseMapper;

    /**
     * 查询商品记录
     */
    @Override
    public ProductVo queryById(Integer productId){
        return baseMapper.selectVoById(productId);
    }

    /**
     * 查询商品记录列表
     */
    @Override
    public TableDataInfo<ProductVo> queryPageList(ProductBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Product> lqw = buildQueryWrapper(bo);
        Page<ProductVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品记录列表
     */
    @Override
    public List<ProductVo> queryList(ProductBo bo) {
        LambdaQueryWrapper<Product> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Product> buildQueryWrapper(ProductBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Product> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSupplierId() != null, Product::getSupplierId, bo.getSupplierId());
        lqw.eq(bo.getBrandId() != null, Product::getBrandId, bo.getBrandId());
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), Product::getProductName, bo.getProductName());
        lqw.eq(bo.getProductPrice() != null, Product::getProductPrice, bo.getProductPrice());
        lqw.eq(bo.getLinePrice() != null, Product::getLinePrice, bo.getLinePrice());
        lqw.eq(bo.getHighPrice() != null, Product::getHighPrice, bo.getHighPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getProductNo()), Product::getProductNo, bo.getProductNo());
        lqw.eq(bo.getProductStock() != null, Product::getProductStock, bo.getProductStock());
        lqw.eq(StringUtils.isNotBlank(bo.getSellingPoint()), Product::getSellingPoint, bo.getSellingPoint());
        lqw.eq(bo.getCategoryId() != null, Product::getCategoryId, bo.getCategoryId());
        lqw.eq(bo.getSpecType() != null, Product::getSpecType, bo.getSpecType());
        lqw.eq(bo.getDeductStockType() != null, Product::getDeductStockType, bo.getDeductStockType());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), Product::getContent, bo.getContent());
        lqw.eq(bo.getProductSort() != null, Product::getProductSort, bo.getProductSort());
        lqw.eq(bo.getIsVirtual() != null, Product::getIsVirtual, bo.getIsVirtual());
        lqw.eq(bo.getIsPicture() != null, Product::getIsPicture, bo.getIsPicture());
        lqw.eq(bo.getMerchantId() != null, Product::getMerchantId, bo.getMerchantId());
        return lqw;
    }

    /**
     * 新增商品记录
     */
    @Override
    public Boolean insertByBo(ProductBo bo) {
        Product add = BeanUtil.toBean(bo, Product.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProductId(add.getProductId());
        }
        return flag;
    }

    /**
     * 修改商品记录
     */
    @Override
    public Boolean updateByBo(ProductBo bo) {
        Product update = BeanUtil.toBean(bo, Product.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Product entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean deleteById(Integer productId) {
        return baseMapper.deleteById(productId) > 0;
    }
}
